<?php

namespace app\admin\model\account;

use think\Model;


class Record extends Model
{
    // 表名
    protected $name = 'order_account_record';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'oar_type_text',
        'oar_account_type_text',
        'oar_status_text',
        'oar_create_time_text'
    ];


    public function saveOrderSettledByStorageId($ids){
        $storage_model        = new \app\common\model\Storage();
        $order_list           = $storage_model->getOrderIds($ids);
        foreach ($order_list as $val){
            $this->saveOrderSettled($val['oi_order_id']);
        }
    }


    public function saveOrderSettled($order_id){
        $order_model             = new \app\admin\model\select\Order();
        $storage_model           = new \app\common\model\Storage();
        $where                   = [];
        $where['oi_order_id']  = $order_id;//获取订单总金额
        $order_money_total       = $storage_model->getPriceTotal($where);
        //获取已转款金额
        $where                  = [];
        $where['oar_order_id']  = $order_id;
        $where['oar_type']      = 1;
        $account_record_total = $this->getMoneyTotal($where);
        if($order_money_total == $account_record_total){
            //记录已结清
            $update = [
                'o_is_settled' => 1,
                'o_update_time' => time()
            ];
        }else{
            $update = [
                'o_is_settled' => 0,
                'o_update_time' => time()
            ];
        }
        $order_model->update($update,['o_id'=>$order_id]);
        return true;
    }

    public function getMoneyTotal($where){
        $where['oar_is_deleted'] = 0;
        $data =  static::where($where)
            ->field('sum(oar_money) as total')
            ->find();
        return $data['total']??0;
    }

    
    public function getOarTypeList()
    {
        return ['1' => __('Oar_type 1'), '2' => __('Oar_type 2'), '3' => __('Oar_type 3'), '4' => __('Oar_type 4'), '5' => __('Oar_type 5')];
    }

    public function getOarAccountTypeList()
    {
        return ['1' => __('Oar_account_type 1'), '2' => __('Oar_account_type 2'), '3' => __('Oar_account_type 3')];
    }

    public function getOarStatusList()
    {
        return ['0' => __('Oar_status 0'), '1' => __('Oar_status 1'), '2' => __('Oar_status 2')];
    }
    public function getStatusList()
    {
        return ['1' => __('Oar_status 1'), '2' => __('Oar_status 2')];
    }


    public function getOarTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['oar_type']) ? $data['oar_type'] : '');
        $list = $this->getOarTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOarAccountTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['oar_account_type']) ? $data['oar_account_type'] : '');
        $list = $this->getOarAccountTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOarStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['oar_status']) ? $data['oar_status'] : '');
        $list = $this->getOarStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOarCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['oar_create_time']) ? $data['oar_create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setOarCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
